﻿using DigDes.DSchool.SUPS.DataAccess.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DigDes.DSchool.SUPS.DataAccess.FileLoaders
{
    public class Loader : ILoader<CarInfo>
    {
        // TODO: Реализовать интерфейс загрузчика и логику проверки типа файла/лоигку загрузки файла

        public bool Check(string filePath)
        {
            using (System.IO.StreamReader file = new System.IO.StreamReader(filePath))
            {
                bool check = (file.ReadLine().Length == 70);
                file.Close();
                return check;
            }
        }

        public IEnumerable<CarInfo> LoadFileRow(string filePath)
        {
            string line;
            System.IO.StreamReader file = new System.IO.StreamReader(filePath);
            while ((line = file.ReadLine()) != null)
            {

                var row = new CarInfo();

                row.CarNumber = line.Substring(0, 8);
                row.CargoWeight = line.Substring(8, 3);
                row.DestinationStation = line.Substring(11, 5);
                row.CargoCode = line.Substring(16, 5);
                row.ConsigneeCode = line.Substring(21, 4);
                row.CarParkType = line.Substring(25, 2);
                row.DepartureStation = line.Substring(27, 5);
                row.OperationCode = line.Substring(32, 2);

                int day = Int32.Parse(line.Substring(34, 2));
                int month = Int32.Parse(line.Substring(36, 2));
                int year = 2000 + Int32.Parse(line.Substring(38, 2));
                int hour = Int32.Parse(line.Substring(40, 2));
                int minute = Int32.Parse(line.Substring(42, 2));
                row.OperationTime = new DateTime(year, month, day, hour, minute, 0);

                row.StationOfOperation = line.Substring(44, 5);
                row.DeliveryRoad = line.Substring(49, 2);
                row.ReceipRoad = line.Substring(51, 2);
                row.TrainIndex = line.Substring(53, 13);
                row.TrainNumber = line.Substring(66, 4);
                yield return row;
            }
            file.Close();
        }
    }
}
